'use strict'

define(['jquery', 'bootstrap', 'select2', 'datetimepicker', 'datetimepickerCN',
        'shiftform', 'validate', 'validate.en','ajaxTab', 'countdown'], function($) {
  
  return {
        
      init: function(data) {

          this.initPage(data.status);
          this.initRefund();
          this.initChangeSalesman();
          this.initInvoice();
          this.initPay();
          this.initDistributionChannel();
          this.initDeposit();
          this.initBalancePayment();
          this.initOffer();
          this.initCreateYear();

      },
      
      initPage: function(status){
      
          $('select[name="siteId"]').select2({
              minimumResultsForSearch : -1,
              allowClear : true,
              width: '150'
            });
      
            $('select[name="siteId"]').change(function(){ 
              var site = $(this).children('option:selected').val();
              window.location.href = window.__CTX__.root + "/year-orders/?siteId=" + site + "&status=" + status;
            });
      },
      
      initRefund: function(){
          $(document).on("shown.bs.modal", "#refund", function(e) {
          var $form = $(e.currentTarget).find("form");
        
          $form.bt_validate();
        
          $form.shiftform({
              beforeSubmit: function(el) {
                $('form').trigger("validate.ajax.submit");
                     return $.bt_validate.result[$.bt_validate.form_id]
              }
          });
          
          })
          
      },
      
      initChangeSalesman: function(){
          $(document).on("shown.bs.modal", "#change-salesman", function(e) {
          var $form = $(e.currentTarget).find("form");
        
          $form.bt_validate();
        
          $form.shiftform({
              beforeSubmit: function(el) {
                $('form').trigger("validate.ajax.submit");
                     return $.bt_validate.result[$.bt_validate.form_id]
              }
          });
          
          })
          
      },
      
      initInvoice: function(){
          $(document).on("shown.bs.modal", "#invoice", function(e) {
          
          	$(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
    	        
    	        var $form = $(e.currentTarget).find("form")                          //bind shiftform
    	        $form.shiftform({
    	          beforeSubmit : function(el) {
    	            $form.trigger("validate.ajax.submit");
    	            return $.bt_validate.result[$.bt_validate.form_id]
    	          }
    	        })
          })
          
      },
      
      initPay: function(){
          $(document).on("shown.bs.modal", "#pay", function(e) {
          var $form = $(e.currentTarget).find("form");
        
          $form.bt_validate();
        
          $form.shiftform({
              beforeSubmit: function(el) {
                $('form').trigger("validate.ajax.submit");
                     return $.bt_validate.result[$.bt_validate.form_id]
              }
          });
          
          })
          
      },
      
      initDistributionChannel: function(){
          $(document).on("shown.bs.modal", "#distribution-channel", function(e) {
          
              $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
                
                var $form = $(e.currentTarget).find("form")                          //bind shiftform
                $form.shiftform({
                  beforeSubmit : function(el) {
                    $form.trigger("validate.ajax.submit");
                    return $.bt_validate.result[$.bt_validate.form_id]
                  }
                })
          })
          
      },
      
      initDeposit: function(){
          $(document).on("shown.bs.modal", "#deposit", function(e) {
          
              $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
                
                var $form = $(e.currentTarget).find("form")                          //bind shiftform
                $form.shiftform({
                  beforeSubmit : function(el) {
                    $form.trigger("validate.ajax.submit");
                    return $.bt_validate.result[$.bt_validate.form_id]
                  }
                })
          })
          
      },
      
      initBalancePayment: function(){
          $(document).on("shown.bs.modal", "#balance-payment", function(e) {
          
              $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
                
                var $form = $(e.currentTarget).find("form")                          //bind shiftform
                $form.shiftform({
                  beforeSubmit : function(el) {
                    $form.trigger("validate.ajax.submit");
                    return $.bt_validate.result[$.bt_validate.form_id]
                  }
                })
          })
          
      },
      
      initOffer: function(){
          $(document).on("shown.bs.modal", "#offer", function(e) {
          
              $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
                
                var $form = $(e.currentTarget).find("form")                          //bind shiftform
                $form.shiftform({
                  beforeSubmit : function(el) {
                    $form.trigger("validate.ajax.submit");
                    return $.bt_validate.result[$.bt_validate.form_id]
                  }
                })
          })
          
      },
      
      /**
       * Bind mobile binding page event
       */
      bindMobileBindingPageEvent: function() {
        var that = this,
          $mobile = $('input[name="mobile"]'),
          $code = $('input[name="code"]');

        // On requesting verify code
        $(document).on('click', '#get-code', function(e) {
          e.preventDefault()
          
          var $button = $(e.currentTarget), mobile = $mobile.val().trim()
          if (that.isMobile(mobile)) {
            // Disable button
            $button.css('pointer-events', 'none')
            // Request verifiy code
            that.requestVerifyCode(mobile, function(data) {
              
              that.token = data.token;
              $('input[name="token"]').val(data.token);
              
              $button.countdown('s后重试', 60, function() {
                $button.css('pointer-events', 'initial').text('获取验证码')
              })
            }, function() {
              $button.css('pointer-events', 'initial')
            })
          } else {
            //$.toast('请输入正确的手机号码')
          	$('input[name="mobile"]').show_err_tooltip("请输入正确的手机号码")
          }
        })
      },

      /**
       * Check mobile value is valid or not
       */
      isMobile: function(mobile) {
        return mobile && /^((13)|(14)|(15)|(16)|(17)|(18)|(19))\d{9}$/.test(mobile)
      },

      /**
       * Request server to send verifiy code to mobile
       */
      requestVerifyCode: function(mobile, success, failure) {
          $.post(__CTX__.root + '/verify-code', { 'mobile': mobile }).done(function(response) {
//              $that.processResult(response, success, function() {
//                $.toast('获取验证码失败')
//                failure && failure()
//              })
          	var result = response
              if (!!result && parseInt(result.code) === 0) {
                success && success(result.data)
              } else if (!!failure) {
              	
                failure(result)
              } else {
                console.warn('No function found handling result: ', result)
              }
            })
//        utils.requestVerifyCode(mobile, function(result) {
//          $.hideIndicator()
//          $.toast('验证码发送成功')
//          success && success(result.data)
//        }, failure)
      },
      
      initCreateYear:function(){
    	var that = this;
        console.log('initCreateYear');
        $(document).on("shown.bs.modal", "#create-year", function(e) {
            console.log('create-give');
            that.bindMobileBindingPageEvent();
            $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
              console.log('bind-give');
              console.log(e);
              console.log(e.currentTarget);

              var $form = $(e.currentTarget).find("form") 
              console.log($form);
                         //bind shiftform
              $form.shiftform({
                beforeSubmit : function(el) {
                  $form.trigger("validate.ajax.submit");
                  return $.bt_validate.result[$.bt_validate.form_id]
                }
              })
        })
    }
        
    }
    })

